package cn.iocoder.yudao.module.system.service.merchant;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.MerchantApplicationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.MerchantApplicationPageRespVO;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.MerchantAuthUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.app.merchant.vo.BusinessBaseApplicationReqVO;
import cn.iocoder.yudao.module.system.controller.app.merchant.vo.BusinessMerchantApplicationVO;
import cn.iocoder.yudao.module.system.convert.merchant.MerchantApplicationRespConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.merchant.MerchantDO;
import cn.iocoder.yudao.module.system.dal.dataobject.merchant.SystemMerchantAuth;
import cn.iocoder.yudao.module.system.dal.mysql.merchant.MerchantAuthMapper;
import org.springframework.stereotype.Service;
import cn.iocoder.yudao.module.system.dal.mysql.merchant.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 梁富贵
 * @Date 2024/10/18
 **/
@Service
public class MerchantAuthServiceImpl implements MerchantAuthService{
    @Resource
    private MerchantAuthMapper merchantAuthMapper;

    @Resource
    private MerchantMapper merchantMapper;


    /**
     * 个体商户申请
     */
    @Override
    public Boolean insertMerchantAuth(BusinessBaseApplicationReqVO businessBase,String requestMappingPath) throws IOException {
        Long userId = WebFrameworkUtils.getLoginUserId();
        if(userId == null || businessBase.getLegalRepresentative()== null
                || businessBase.getIdentifyCard()==null){
            System.out.println("参数错误");
            return false;
        }
        SystemMerchantAuth systemMerchantAuth = BeanUtils.toBean(businessBase, SystemMerchantAuth.class);
        systemMerchantAuth.setUserId(userId);

        this.insertObjectFill(systemMerchantAuth);
        return merchantAuthMapper.insert(systemMerchantAuth) > 0;
    }

    /**
     * 企业商户申请
     */
    @Override
    public Boolean insertMerchantAuth(BusinessMerchantApplicationVO businessMerchant, String requestMappingPath) throws IOException {
        Long userId = WebFrameworkUtils.getLoginUserId();
        if(userId == null|| businessMerchant.getLegalRepresentative()== null
                || businessMerchant.getIdentifyCard()==null){
            return false;
        }
        SystemMerchantAuth systemMerchantAuth = BeanUtils.toBean(businessMerchant, SystemMerchantAuth.class);

        systemMerchantAuth.setUserId(userId);
        this.insertObjectFill(systemMerchantAuth);

        return merchantAuthMapper.insert(systemMerchantAuth)>0;
    }

    /**
     * 分页查询商户申请信息
     */
    @Override
    public PageResult<MerchantApplicationPageRespVO> selectPage(MerchantApplicationPageReqVO reqVO) {
        PageResult<SystemMerchantAuth> pageResult = merchantAuthMapper.selectPageByReqVO(reqVO);
        MerchantApplicationRespConvert convert = new MerchantApplicationRespConvert();
        return new PageResult<>(convert.convertList(pageResult.getList()), pageResult.getTotal());
    }

    /**
     * 通过审核
     */
    @Override
    public Boolean passedMerchantAuth(MerchantAuthUpdateReqVO reqVO) {
        SystemMerchantAuth systemMerchantAuth = merchantAuthMapper.selectByUserIdAndId(reqVO);
        MerchantDO merchantDO = new MerchantDO();
        merchantDO.setMerchantId(systemMerchantAuth.getUserId());
        merchantDO.setMerchantName(systemMerchantAuth.getMerchantName());
        merchantDO.setMerchantIdentity(systemMerchantAuth.getIdentify());
        merchantDO.setBusinessLicense(systemMerchantAuth.getBusinessLicense());
        merchantDO.setLegalRepresentative(systemMerchantAuth.getLegalRepresentative());
        merchantDO.setLogo(systemMerchantAuth.getLogo());
        merchantDO.setContactPhone(systemMerchantAuth.getContactPhone());
        merchantDO.setIdentityCard(systemMerchantAuth.getIdentifyCard());
        merchantDO.setUpdater(WebFrameworkUtils.getLoginUserId().toString());
        return merchantMapper.insert(merchantDO)>0&&merchantAuthMapper.passedMerchantAuth(reqVO);
    }

    /**
     * 驳回申请
     */
    @Override
    public Boolean rejectMerchantAuth(MerchantAuthUpdateReqVO reqVO) {
        return merchantAuthMapper.rejectMerchantAuth(reqVO);
    }


    /**
     * 插入数据时，填充字段
     */
    private SystemMerchantAuth insertObjectFill(SystemMerchantAuth systemMerchantAuth){
        systemMerchantAuth.setCertificationDate(LocalDateTime.now());
        systemMerchantAuth.setCertificationStatus(0);
        return systemMerchantAuth;
    }

}
